package server.business.login.handler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.security.auth.login.LoginContext;

import server.ScriptEnum;
import server.ServerEngin;
import server.ServerHandler;
import server.business.login.ILoginScript;
import server.business.login.LoginScript;
import server.business.login.data.LoginStatusEnum;
import server.commonData.SqlStatementEnum;
import server.commonData.User;
import server.commonData.Utils;
import server.messageId.S2CMessageId;
import server.msgBean.LoginMessage.LoginReq;
import server.msgBean.LoginMessage.LoginRes;
import server.msgBean.LoginMessage.RoomInfoList;
import server.utils.AbsAsyncQueryDb;

import com.core.NetChannel;
import com.core.initialize.Message;
import com.google.protobuf.GeneratedMessage;

/**
 * 请求登陆
 * @author King
 *
 */
public class LoginHandler extends ServerHandler {

	@Override
	public Class<? extends GeneratedMessage> initBodyClass()
	{
		return LoginReq.class;
	}

	@Override
	protected void handler(Message msg, User user) throws Throwable
	{
		LoginReq req = msg.getBody();
		List<String> params = new ArrayList<String>();
		params.add(req.getAccount());
		new QueryAcoount(SqlStatementEnum.QUERY_USERBYACCOUNT, params, msg.getChannel(),req.getPassword());
	}
	/***
	 * 异步查询数据库
	 * @author King
	 *
	 */
	class QueryAcoount extends AbsAsyncQueryDb
	{
		private NetChannel channel;
		private String password;
		
		public QueryAcoount(String sql, List params,NetChannel channel,String password) throws Exception
		{
			super(sql, params);
			this.channel = channel;
			this.password = password;
			query(sql, params);
			doCallBack();
		}

		@Override
		protected void queryBack()
		{
			//在查询的过程中连接关闭了
			if(!channel.getChannel().isWritable())
				return;
			LoginRes.Builder builder = LoginRes.newBuilder();
			Message msg = Message.newMessage();
			msg.setModuleAction(S2CMessageId.Login.MODULE_ID, S2CMessageId.Login.LOGIN_RES);
			//帐号不存在
			if(this.result.isEmpty())
			{
				builder.setUserStatus(LoginStatusEnum.ACCOUNT_NOTEXIST);
				msg.setBody(builder);
				channel.write(msg);
				return;
			}
			Map<String, Object> map = result.get(0);
//			String password = (String)map.get("password");
//			//密码错误
//			if(!password.equals(this.password))
//			{
//				builder.setUserStatus(LoginStatusEnum.PASSWORD_ERROR);
//				msg.setBody(builder);
//				channel.write(msg);
//				return;
//			}
			//帐号正常 
			User user = ServerEngin.getInstance().getCache().getUser(Utils.readLong(map, "id"));
			//顶号
			if(user!=null)
			{
				//先把老玩家踢下线
				ILoginScript script = ServerEngin.getInstance().getScriptM().getScript(ScriptEnum.LOGIN_SCRIPT);
				script.quiteGame(user, false);
				user.getChannel().setKick(true);
				user.getChannel().getChannel().close();
				//设置新玩家网络
				user.setChannel(channel);
			}else
			{
				//没有顶号
				user = new User(this.channel);
				user.readFromDb(map);
			}
			ServerEngin.getInstance().getCache().addUser(user);
			//设置玩家信息
			builder.setFaceIndex(user.getFace());
			builder.setMoney(user.getMoney());
			builder.setYuanbao(user.getYuanbao());
			builder.setVipLevel(user.getVipLevel());
			//设置房间信息
			RoomInfoList.Builder roomBuilder = RoomInfoList.newBuilder();
			ServerEngin.getInstance().getCache().getRoomManager().showRoomInfo(roomBuilder);
			builder.setRoomList(roomBuilder);
			msg.setBody(builder);
			channel.write(msg);
		}
	}

}
